package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.entity.response.ResponseHeader;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class SealComparisonActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etSealOne)
    FreeUI_ClearEditText etSealOne;
    @BindView(R.id.etSealTwo)
    FreeUI_ClearEditText etSealTwo;
    @BindView(R.id.etSealThree)
    FreeUI_ClearEditText etSealThree;
    @BindView(R.id.etSealFour)
    FreeUI_ClearEditText etSealFour;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flComparisonTime)
    FrameLayout flComparisonTime;
    @BindView(R.id.tvComparisonTime)
    TextView tvComparisonTime;
    @BindView(R.id.ivComparisonTime)
    ImageView ivComparisonTime;
    @BindView(R.id.flChoice)
    FrameLayout flChoice;
    @BindView(R.id.tvChoice)
    TextView tvChoice;
    @BindView(R.id.ivChoice)
    ImageView ivChoice;
    private FreeUI_GeneralPop mComparisonTimePop;
    private ArrayList<String> comparisonTimeList = new ArrayList<>();
    private FreeUI_GeneralPop mChoicePop;
    private ArrayList<String> choiceList = new ArrayList<>();
    private String choice = "ZY";
    private String comparisonTime = "SJ";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_seal_comparison;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        tvTitle.setText("印章比对");

        choiceList.add("正印");
        choiceList.add("背印");
        comparisonTimeList.add("首检");
        comparisonTimeList.add("自检");
        comparisonTimeList.add("尾检");

//        initEdit(etLotID, s -> {
//            LotUtils.searchLotInfo(SealComparisonActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
//                @Override
//                public void onSuccess(LotInfoEntity response) {
//                    lotInfoEntity = response;
//                }
//
//                @Override
//                public void onFail(Throwable t) {
//                    loadError(t,"LOTQUERY");
//                }
//            });
//
//        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.flComparisonTime,R.id.flChoice,R.id.tvComparison,R.id.tvClone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvClone:
                etLotID.setText("");
                tvComparisonTime.setText("首检");
                tvChoice.setText("正印");
                etSealOne.setText("");
                etSealTwo.setText("");
                etSealThree.setText("");
                etSealFour.setText("");
                choice = "ZY";
                comparisonTime = "SJ";
                break;
            case R.id.flComparisonTime:
                if (mComparisonTimePop != null) {
                    if (mComparisonTimePop.isShowing()) {
                        mComparisonTimePop.dismiss();
                    }
                } else {
                    mComparisonTimePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flComparisonTime.getWidth(), comparisonTimeList, ivComparisonTime, (codeS, i) -> {
                        tvComparisonTime.setText(codeS);
                        switch (codeS){
                            case "首检":
                                comparisonTime = "SJ";
                                break;
                            case "自检":
                                comparisonTime = "ZJ";
                                break;
                            case "尾检":
                                comparisonTime = "WJ";
                                break;
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivComparisonTime);
                mComparisonTimePop.showAsDropDown(flComparisonTime, 0, 0);
                break;
            case R.id.flChoice:
                if (mChoicePop != null) {
                    if (mChoicePop.isShowing()) {
                        mChoicePop.dismiss();
                    }
                } else {
                    mChoicePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flChoice.getWidth(), choiceList, ivChoice, (codeS, i) -> {
                        tvChoice.setText(codeS);
                        switch (codeS){
                            case "正印":
                                choice = "ZY";
                                break;
                            case "背印":
                                choice = "BY";
                                break;
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivChoice);
                mChoicePop.showAsDropDown(flChoice, 0, 0);
                break;
            case R.id.tvComparison:
                FreeUI_EntityCallBack<BaseResponseBean<ResponseHeader>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseHeader>>
                        (new TypeToken<BaseResponseBean<ResponseHeader>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<ResponseHeader>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(SealComparisonActivity.this,false,getSupportFragmentManager())){
                            if (response.body().getResponse().getBody().getRESULT().equals("OK")) {
                                showToast("比对完成", true);
                            }else {
                                showToast(response.body().getResponse().getBody().getRESULT(), true);
                            }
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<ResponseHeader>> response) {
                        super.onError(response);
                        loadError(response.getException(),"SEALCOMPARISON");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","SEALCOMPARISON");
                map.addProperty("LOTID",getText(etLotID));
                map.addProperty("SEAL",choice);
                map.addProperty("OPPORTUNITY",comparisonTime);
                map.addProperty("FIRSTLINE",getText(etSealOne));
                map.addProperty("SECONDLINE",getText(etSealTwo));
                map.addProperty("THIRDLINE",getText(etSealThree));
                map.addProperty("FOURTHLINE",getText(etSealFour));
                NetUtils.requestPostNet("CPC.SEALCOMPARISON","TRANSFERMES",SealComparisonActivity.this,map,callBack);
                break;
        }
    }
}